properties {
    dockerHost 'tcp://192.168.99.100:2376'
    dockerCert '/Users/robert/.docker/machine/machines/default'
    dockerImage 'ubuntu:15.04'
}

step pull(type: 'greenmoonsoftware.tidewater.plugins.docker.DockerPull') {
    uri context.dockerHost //Properties are accessible via the context variable
    certPath context.dockerCert
    image context.dockerImage
}

step start(type: 'greenmoonsoftware.tidewater.plugins.docker.DockerStart') {
    uri context.dockerHost
    certPath context.dockerCert
    image context.dockerImage
    waitForCompletion false
    command (['/bin/sh', '-c', 'while true; do echo hello world; sleep 5; done'])
}

step sleep { //If no type is provided for the step, then plain 'ol groovy code is executed.
    def millisecondsToSleep = 10000
    println "Going to sleep for ${millisecondsToSleep / 1000} seconds..."
    sleep 10000
    println 'I am awake'
}

step stop(type: 'greenmoonsoftware.tidewater.plugins.docker.DockerStop') {
    uri context.dockerHost
    certPath context.dockerCert
    containerId start.containerId //Steps may have output variables. These are referenced by their step name. ie: "start"
}

step logs(type: 'greenmoonsoftware.tidewater.plugins.docker.DockerLogs') {
    uri context.dockerHost
    certPath context.dockerCert
    containerId start.containerId
    toFilePath "${context.workspace}/logs.txt" //Each individual execution of a pipeline (ie: context) has it's own workspace directory. No file system collisions with concurrent executions.
}

step catDockerLogToPipeline(type: 'greenmoonsoftware.tidewater.plugins.shell.Shell') {
    env (['fileToCat': logs.toFilePath]) //Previous step inputs are available using their step name. ie: "logs"
    contents '''\\
        #!/bin/bash
        cat $fileToCat
    '''.stripIndent()
}

step tag(type: 'greenmoonsoftware.tidewater.plugins.docker.DockerTag') {
    uri context.dockerHost
    certPath context.dockerCert
    imageToTag context.dockerImage
    repository 'local.docker:5000/tidewater/ubuntu'
    tag "${new Date().format('yyyyMMddHHmmss')}"
}

step push(type: 'greenmoonsoftware.tidewater.plugins.docker.DockerPush') {
    uri context.dockerHost
    certPath context.dockerCert
    image tag.repository
}